import 'package:mall/constant/app_parameters.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/app_urls.dart';
import 'package:mall/modules/model/user_entity.dart';
import 'package:mall/utils/net/http_util.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../model/account.dart';

class UserService {
  //获取验证码
  static Future<void> getCode(String mobile) async {
    var parameters = {'mobile': mobile};
    await HttpUtil.instance.post(AppUrls.REG_CAPTCHA, parameters: parameters);
  }

  //账号密码登录
  static Future<UserEntity> login(String account, String passWord) async {
    var parameters = {AppParameters.USER_NAME: account, AppParameters.PASS_WORD: passWord};

    final response = await HttpUtil.instance.post(AppUrls.LOGIN, parameters: parameters);
    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }
    //保存账号密码
    await saveAccount(account, passWord);
    //保存用户信息到本地
    final userEntity = UserEntity.fromJson(response[AppStrings.DATA]);
    await _saveUserInfo(userEntity);
    return userEntity;
  }

  //注册
  static Future<void> register(String account, String passWord) async {
    var parameters = {
      AppParameters.USER_NAME: account,
      AppParameters.PASS_WORD: passWord,
      AppParameters.MOBILE: account,
      AppParameters.CODE: "8888"
    };
    await HttpUtil.instance.post(AppUrls.REGISTER, parameters: parameters);
  }

  //注册
  static Future<void> registerAndLogin({
    String? account,
    String? passWord,
    required String mobile,
    required String code,
  }) async {
    var parameters = {
      AppParameters.USER_NAME: account,
      AppParameters.PASS_WORD: passWord,
      AppParameters.MOBILE: account,
      AppParameters.CODE: code
    };
    await HttpUtil.instance.post(AppUrls.REGISTER, parameters: parameters);
  }

  //获取用户信息
  static Future<UserUserinfo?> getUserInfo() async {
    //头像
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    //判断本地是否有用户信息
    if (sharedPreferences.getString(AppStrings.HEAD_URL) == null) {
      return null;
    }
    return UserUserinfo(
      avatarUrl: sharedPreferences.getString(AppStrings.HEAD_URL),
      nickName: sharedPreferences.getString(AppStrings.NICK_NAME),
    );
  }

  //保存用户信息到本地
  static _saveUserInfo(UserEntity userEntity) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    await sharedPreferences.setString(AppStrings.TOKEN, userEntity.token ?? "").then((value) => print(value));
    await sharedPreferences.setString(AppStrings.HEAD_URL, userEntity.userInfo?.avatarUrl ?? "");
    await sharedPreferences.setString(AppStrings.NICK_NAME, userEntity.userInfo?.nickName ?? "");
  }

  //退出登录
  static Future<void> logout() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    //清除头像
    await sharedPreferences.remove(AppStrings.HEAD_URL);
    //清除用户信息
    await sharedPreferences.remove(AppStrings.NICK_NAME);
    //清除token
    await sharedPreferences.remove(AppStrings.TOKEN);
  }

  //用户是否登录
  static Future<bool> isLogin() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.getString(AppStrings.TOKEN) != null;
  }

  //保存账号和密码
  static Future<void> saveAccount(String account, String passWord) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    await sharedPreferences.setString(AppStrings.USER_NAME, account);
    await sharedPreferences.setString(AppStrings.PASS_WORD, passWord);
  }

  //获取账号和密码
  static Future<Account?> getAccount() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    if (sharedPreferences.getString(AppStrings.USER_NAME) == null ||
        sharedPreferences.getString(AppStrings.USER_NAME)!.isEmpty) {
      return null;
    }
    if (sharedPreferences.getString(AppStrings.PASS_WORD) == null ||
        sharedPreferences.getString(AppStrings.PASS_WORD)!.isEmpty) {
      return null;
    }
    return Account(
      account: sharedPreferences.getString(AppStrings.USER_NAME)!,
      passWord: sharedPreferences.getString(AppStrings.PASS_WORD)!,
    );
  }
}
